﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartSpace.Core
{
    public partial class CoreService :ICoreService
    {
        public bool SaveUserGroup(UserGroup group)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                EntityRepository.Save(group, context);

                return EntityRepository.Commit(context);
            }
        }

        public bool DeleteUserGroup(Guid groupId)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                if (!EntityRepository.DeleteCodeEntity<UserGroup>(groupId, context))
                {
                    return false;
                }
                return EntityRepository.Commit(context);
            }
        }

        public UserGroup GetUserGroup(Guid groupId)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<UserGroup>()
                    .Include("Users")
                    .Where(p => p.Id == groupId)
                    .SingleOrDefault();
            }
        }

        public List<UserGroup> GetUserGroups()
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                return context.Set<UserGroup>()
                    .OrderBy(p => p.SortIndex)
                    .ToList();
            }
        }

        public PagedList<User> GetUserGroupContainUsers(Guid id, int start, int limit)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                string sql = "select u.* from __user as u join __RelationUserToUserGroup as r on r.userid=u.id where r.usergroupid={0} order by u.account";
                int total = context.Database.SqlQuery<User>(sql, id).Count();
                List<User> users = context.Database.SqlQuery<User>(sql, id).Take(limit).Skip(start).ToList();

                PagedList<User> pagedUser = new PagedList<User>();
                pagedUser.AddRange(users);
                pagedUser.Start = start;
                pagedUser.Limit = limit;
                pagedUser.Total = total;

                return pagedUser;
            }
        }

        public bool AssignUserToUserGroup(Guid userId, Guid groupId)
        {
            using (CoreDbContext context = new CoreDbContext())
            {
                UserGroupRelation relation = new UserGroupRelation();
                relation.Id = Guid.NewGuid();
                relation.UserId = userId;
                relation.GroupId = groupId;

                if (context.Set<UserGroupRelation>().Where(p => p.UserId == userId && p.GroupId == groupId).Select(p => p.Id).SingleOrDefault() == default(Guid))
                {
                    context.Set<UserGroupRelation>().Add(relation);
                }
                else
                {
                    context.Entry(relation).State = System.Data.Entity.EntityState.Modified;
                }

                if (context.SaveChanges() > 0)
                {
                    return true;
                }
                return false;
            }
        }
    }
}
